<template>
  <el-card class="card-box">
    <el-row slot="header" type="flex" class="clearfix">
      <el-col>
        <el-button  type="primary" size="small" @click="showDialog=true">添加停车位</el-button>
      </el-col>
      <el-col>
        <el-row type="flex">
          <el-select v-model="searchData.parkingId" placeholder="请选停车场" @change="search">
            <el-option v-for="item in parking_lot_list" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
          <el-select v-model="searchData.isChargingPile" placeholder="请选择是否有充电桩" @change="search">
            <el-option label="全部" :value="null" />
            <el-option label="有" :value="1" />
            <el-option label="无" :value="0" />
          </el-select>
          <el-select v-model="searchData.isFree" placeholder="请选择是否有空闲" @change="search">
            <el-option label="全部" :value="null" />
            <el-option label="是" :value="0" />
            <el-option label="否" :value="1" />
          </el-select>
        </el-row>
      </el-col>
    </el-row>
    <el-table border :data="formData">
      <el-table-column
        prop="id"
        label="id"
      />
      <el-table-column
        prop="startTime"
        label="停车开始时间"
      />
      <el-table-column
        prop="licensePlateNumber"
        label="车牌号"
      />
      <el-table-column
        prop="isChargingPile"
        label="充电桩"
      >
      <template slot-scope="scope">
        <span v-if="scope.row.isChargingPile === 1">有</span>
        <span v-if="scope.row.isChargingPile === 0">无</span>
      </template>
    </el-table-column>
      <el-table-column
        prop="isFree"
        label="空闲状态"
      >
      <template slot-scope="scope">
        <span v-if="scope.row.isFree === 0">是</span>
        <span v-if="scope.row.isFree === 1">否</span>
      </template>
    </el-table-column>
      <el-table-column
        prop="name"
        label="停车场"
      />
      <el-table-column
        label="操作"
        width="250"
      >
        <template v-slot="{row}">
          <el-button  type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
          <el-button v-if="row.startTime != null"  type="primary" size="small" @click="issueSpace(row.id)">结算</el-button>
          <el-button type="danger" size="small" @click="delFn(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="center">
      <el-pagination
        layout="prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page="page"
        @current-change="pageChange"
      />
    </el-row>
    <AddProperties
      ref="addProperties"
      :show-dialog.sync="showDialog"
      @updateFn='getTotal'
    />
  </el-card>
</template>

<script>
import { getSpaceList, getParkingLotList, del, issue} from '../../api/space'
import AddProperties from './components/add-properties.vue'
export default {
  components: { AddProperties },
  data() {
    return {
      formData: [{}],
      limit: 5,
      start: 0,
      showDialog: false,
      total: 0,
      agent: null,
      category: null,
      title_contains: null,
      parking_lot_list: [
        {"id": null, "name": "全部停车场"}
      ], // 停车场下拉列表
      parking: null,
      charging_pile: null,
      free: null,
      page: 1,
      parking_lot: null,
      searchData: []
    }
  },
  async created() {
    // this.getProperties()
    this.getTotal()
    // this.getAgent()
    // this.getCategory()
    this.parkingLotList()
  },
  methods: {
    // 信息
    async getTotal() {
      const res = await getSpaceList({
        page: this.page,
        pageSize: this.limit,
      })
      this.formData = res.data.data.records
      this.total = res.data.data.total
      console.log('category', this.formData)
      console.log('total', this.total)
    },
    async parkingLotList() {
      const res = await getParkingLotList()
      console.log("res.data", res.data.data)
      // this.parking_lot_list = res.data.data
      // var getList = res.data.data
      var parkinglot = []
      parkinglot.push(
        {"id": null, "name": "全部停车场"}
      )
      for (var i = 0; i < res.data.data.length; i ++) {
        console.log(res.data.data[i])
        var message = {
          "id": res.data.data[i].id,
          "name": res.data.data[i].name
        }
        console.log("message", message)
        parkinglot.push(message)
      }
      this.parking_lot_list = parkinglot
    },
    async editFn(id) {
      // const res = await getPropertiesById(id)
      // console.log('房产', res)
      // this.$refs.addProperties.formData = res.data
      this.$refs.addProperties.getInfo(id)
      // this.getTotal()                     
      this.showDialog = true
    },
    async delFn(id) {
      const data = {
        id: id
      }
      await del(data)
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getTotal()
      // console.log(id)
    },
    async issueSpace(id) {
      const data = {
        id: id
      }
      await issue(data)
      this.$message({
        message: '结算成功',
        type: 'success'
      })
      this.getTotal()
      // console.log(id)
    },
    pageChange(page) {
      this.page = page
      this.getTotal()
    },
    // 搜索
    async search() {
      const params = {
        page: this.page,
        pageSize: this.limit,
        parkingId: this.searchData.parkingId,
        isChargingPile: this.searchData.isChargingPile,
        isFree: this.searchData.isFree
      }
      const res = await getSpaceList(params)
      this.formData = res.data.data.records
      this.total = res.data.data.total
      console.log('category', this.formData)
      console.log('total', this.total)
      // this.parkingLotList()
    },
    async getProperties() {
      const res = await getPropertiesList({
        _limit: this.limit,
        _start: this.start,
        title_contains: this.title_contains,
        agent: this.agent,
        category: this.category
      })
      // console.log(res)
      this.formData = res.data
      this.getTotal()
      // console.log('total', this.total)
    },
    // async getAgent() {
    //   const res = await getAgentList()
    //   this.agentList = res.data
    // },
    async getCategory() {
      // const res = await getCategoryList()
      // this.categoryList = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
.card-box{
  margin: 20px;
}
</style>
